package com.example.kt_temp.ui.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.example.kt_temp.model.entity.VideoEntity
import com.example.kt_temp.model.service.VideoService
import java.time.Duration

class VideoViewModel:ViewModel() {

    private val videoService = VideoService.instance()

    var list by mutableStateOf(
        listOf(
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner3.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner3.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner1.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner3.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner2.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner3.webp"
            ),
            VideoEntity(
                title = "行测老搭档，爱上方面看大理石的大萨达",
                type = "视频课程",
                duration = "00:02:11",
                imageUrl = "https://docs.bughub.icu/compose/assets/banner3.webp"
            ),

            )
    )
        private set

    private val pageSize =10
    private var pageOffset =1

    var refreshing by mutableStateOf(false)
        private set

    private var hasMore =false

    var listLoaded by mutableStateOf(false)
        private set

    suspend fun fetchList() {
       val res = videoService.list(pageOffset,pageSize)
        if(res.code==0 && res.data!=null){
            val tempList = mutableListOf<VideoEntity>()
            if(pageOffset !=1){
                tempList.addAll(list)
            }
            tempList.addAll(res.data)
            hasMore = res.data.size == pageSize
            list =tempList
            listLoaded = true
            list = tempList
            refreshing =false
        }
    }

    suspend fun refresh(){
        pageOffset =1
        refreshing =true
        fetchList()
    }

    suspend fun loadMore(){
        pageOffset++
        refreshing =true
        fetchList()
    }

    var coverUrl by mutableStateOf("https://docs.bughub.icu/compose/assets/banner3.webp")
        private set

    var videoUrl by mutableStateOf("https://v-cdn.zjol.com.cn/280443.mp4")
        private set

   private var videoTitle by mutableStateOf("我真的超级爱77小姐姐")

    //HTML 头部
    private val htmlHeader = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title></title>
            <style>
                img {
                    max-width: 100% !important;
                }
            </style>
        </head>
        <body>
    """.trimIndent()

    //html尾部
    private val htmlFooter = """
        </body>
        </html>
    """.trimIndent()

    var infoLoaded by mutableStateOf(false)
        private set

    var videoDesc by mutableStateOf("""${htmlHeader}<h5>${videoTitle}</h5><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div><div><h1>我真的超级爱77小姐姐</h1></div>${htmlFooter}""".trimIndent())
        private set

    suspend fun fetchInfo(){
       val res = videoService.info("")
        if(res.code == 0 && res.data!=null){
            val videoEntity = res.data
            Log.i("结果", "${videoEntity.desc}")
            videoTitle =videoEntity.title
            coverUrl=videoEntity.imageUrl
            videoUrl =videoEntity.video ?: ""
            videoDesc="""$htmlHeader<h5 style="color:#333333;font-size:16px;">${videoTitle}</h5>${videoEntity.desc}$htmlFooter""".trimIndent()
            infoLoaded = true
        }
    }
}